package com.example.wangzhenyu;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import android.widget.TextView;

import com.example.read.R;

public class DetailActivity extends AppCompatActivity {
    private MainViewModel viewModel;
    private TextView titleTextView;
    private TextView contentTextView;
    private TextView explanationTextView;
    private TextView appreciationTextView;
    private TextView authorTextView;

    public static void start(AppCompatActivity activity, int detailid) {
        Intent intent = new Intent(activity, DetailActivity.class);
        intent.putExtra("detailid", detailid);
        activity.startActivity(intent);
    }

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail2);

        titleTextView = findViewById(R.id.title_text_view);
        contentTextView = findViewById(R.id.content_text_view);
        explanationTextView = findViewById(R.id.explanation_text_view);
        appreciationTextView = findViewById(R.id.appreciation_text_view);
        authorTextView = findViewById(R.id.author_text_view);

        viewModel = new ViewModelProvider(this).get(MainViewModel.class);

        int detailid = getIntent().getIntExtra("detailid", -1);
        if (detailid != -1) {
            viewModel.getDetail("b56758b8f0fcbcc3", detailid).observe(this, detail -> {
                titleTextView.setText(detail.getTitle());
                contentTextView.setText(detail.getContent());
                explanationTextView.setText(detail.getExplanation());
                appreciationTextView.setText(detail.getAppreciation());
                authorTextView.setText(detail.getAuthor());
            });
        }
    }
}